{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "19062701",
   "metadata": {},
   "source": [
    "## Cogniswitch Tools\n",
    "\n",
    "**Use CogniSwitch to build production ready applications that can consume, organize and retrieve knowledge flawlessly. Using the framework of your choice, in this case Langchain CogniSwitch helps alleviate the stress of decision making when it comes to, choosing the right storage and retrieval formats. It also eradicates reliability issues and hallucinations when it comes to responses that are generated. Get started by interacting with your knowledge in just two simple steps.**\n",
    "\n",
    "visit [https://www.cogniswitch.ai/developer to register](https://www.cogniswitch.ai/developer?utm_source=langchain&utm_medium=langchainbuild&utm_id=dev).\n",
    "\n",
    "**Registration:** \n",
    "\n",
    "- Signup with your email and verify your registration \n",
    "\n",
    "- You will get a mail with a platform token and oauth token for using the services.\n",
    "\n",
    "\n",
    "\n",
    "**step 1: Instantiate the toolkit and get the tools:**\n",
    "\n",
    "- Instantiate the cogniswitch toolkit with the cogniswitch token, openAI API key and OAuth token and get the tools. \n",
    "\n",
    "**step 2: Instantiate the agent with the tools and llm:**\n",
    "- Instantiate the agent with the list of cogniswitch tools and the llm, into the agent executor.\n",
    "\n",
    "**step 3: CogniSwitch Store Tool:** \n",
    "\n",
    "***CogniSwitch knowledge source file tool***\n",
    "- Use the agent to upload a file by giving the file path.(formats that are currently supported are .pdf, .docx, .doc, .txt, .html)  \n",
    "- The content from the file will be processed by the cogniswitch and stored in your knowledge store. \n",
    "\n",
    "***CogniSwitch knowledge source url tool***\n",
    "- Use the agent to upload a URL.  \n",
    "- The content from the url will be processed by the cogniswitch and stored in your knowledge store. \n",
    "\n",
    "**step 4: CogniSwitch Status Tool:**\n",
    "- Use the agent to know the status of the document uploaded with a document name.\n",
    "- You can also check the status of document processing in cogniswitch console. \n",
    "\n",
    "**step 5: CogniSwitch Answer Tool:**\n",
    "- Use the agent to ask your question.\n",
    "- You will get the answer from your knowledge as the response. \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfe4510e",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install -qU langchain-community"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1435b193",
   "metadata": {},
   "source": [
    "### Import necessary libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8d86323b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "import os\n",
    "\n",
    "from langchain.agents.agent_toolkits import create_conversational_retrieval_agent\n",
    "from langchain_community.agent_toolkits import CogniswitchToolkit\n",
    "from langchain_openai import ChatOpenAI"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e6acf0e",
   "metadata": {},
   "source": [
    "### Cogniswitch platform token, OAuth token and OpenAI API key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3d2dfc9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "cs_token = \"Your CogniSwitch token\"\n",
    "OAI_token = \"Your OpenAI API token\"\n",
    "oauth_token = \"Your CogniSwitch authentication token\"\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = OAI_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "320e02fc",
   "metadata": {},
   "source": [
    "### Instantiate the cogniswitch toolkit with the credentials"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "89f58167",
   "metadata": {},
   "outputs": [],
   "source": [
    "cogniswitch_toolkit = CogniswitchToolkit(\n",
    "    cs_token=cs_token, OAI_token=OAI_token, apiKey=oauth_token\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16901682",
   "metadata": {},
   "source": [
    "### Get the list of cogniswitch tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "288d07f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "tool_lst = cogniswitch_toolkit.get_tools()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4aae43a3",
   "metadata": {},
   "source": [
    "### Instantiate the llm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4d67e5bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = ChatOpenAI(\n",
    "    temperature=0,\n",
    "    openai_api_key=OAI_token,\n",
    "    max_tokens=1500,\n",
    "    model_name=\"gpt-3.5-turbo-0613\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04179282",
   "metadata": {},
   "source": [
    "### Create a agent executor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2153e758",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent_executor = create_conversational_retrieval_agent(llm, tool_lst, verbose=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42c9890e",
   "metadata": {},
   "source": [
    "### Invoke the agent to upload a URL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "794b4fba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The URL https://cogniswitch.ai/developer has been uploaded successfully. The status of the document is currently being processed. You will receive an email notification once the processing is complete.\n"
     ]
    }
   ],
   "source": [
    "response = agent_executor.invoke(\"upload this url https://cogniswitch.ai/developer\")\n",
    "\n",
    "print(response[\"output\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "544fe8f9",
   "metadata": {},
   "source": [
    "### Invoke the agent to upload a File"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fd0addfc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The file example_file.txt has been uploaded successfully. The status of the document is currently being processed. You will receive an email notification once the processing is complete.\n"
     ]
    }
   ],
   "source": [
    "response = agent_executor.invoke(\"upload this file example_file.txt\")\n",
    "\n",
    "print(response[\"output\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02827e1b",
   "metadata": {},
   "source": [
    "### Invoke the agent to get the status of a document"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f424e6c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The status of the document example_file.txt is as follows:\n",
      "\n",
      "- Created On: 2024-01-22T19:07:42.000+00:00\n",
      "- Modified On: 2024-01-22T19:07:42.000+00:00\n",
      "- Document Entry ID: 153\n",
      "- Status: 0 (Processing)\n",
      "- Original File Name: example_file.txt\n",
      "- Saved File Name: 1705950460069example_file29393011.txt\n",
      "\n",
      "The document is currently being processed.\n"
     ]
    }
   ],
   "source": [
    "response = agent_executor.invoke(\"Tell me the status of this document example_file.txt\")\n",
    "\n",
    "print(response[\"output\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ba9aca9",
   "metadata": {},
   "source": [
    "### Invoke the agent with query and get the answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e73e963f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CogniSwitch can help develop GenAI applications in several ways:\n",
      "\n",
      "1. Knowledge Extraction: CogniSwitch can extract knowledge from various sources such as documents, websites, and databases. It can analyze and store data from these sources, making it easier to access and utilize the information for GenAI applications.\n",
      "\n",
      "2. Natural Language Processing: CogniSwitch has advanced natural language processing capabilities. It can understand and interpret human language, allowing GenAI applications to interact with users in a more conversational and intuitive manner.\n",
      "\n",
      "3. Sentiment Analysis: CogniSwitch can analyze the sentiment of text data, such as customer reviews or social media posts. This can be useful in developing GenAI applications that can understand and respond to the emotions and opinions of users.\n",
      "\n",
      "4. Knowledge Base Integration: CogniSwitch can integrate with existing knowledge bases or create new ones. This allows GenAI applications to access a vast amount of information and provide accurate and relevant responses to user queries.\n",
      "\n",
      "5. Document Analysis: CogniSwitch can analyze documents and extract key information such as entities, relationships, and concepts. This can be valuable in developing GenAI applications that can understand and process large amounts of textual data.\n",
      "\n",
      "Overall, CogniSwitch provides a range of AI-powered capabilities that can enhance the development of GenAI applications by enabling knowledge extraction, natural language processing, sentiment analysis, knowledge base integration, and document analysis.\n"
     ]
    }
   ],
   "source": [
    "response = agent_executor.invoke(\"How can cogniswitch help develop GenAI applications?\")\n",
    "\n",
    "print(response[\"output\"])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain_repo",
   "language": "python",
   "name": "langchain_repo"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
